package com.jy.framework.base.utils;

import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
@Slf4j
public class DateUtil {

    private static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");

    public static boolean compareTo(Integer minute, Date dateNew, Date dateOld) {
        Calendar calendarNew = Calendar.getInstance();
        Calendar calendar = Calendar.getInstance();
        calendarNew.setTime(dateNew);
        calendar.setTime(dateOld);
        calendarNew.add(Calendar.MINUTE, -1 * minute);
        return calendar.compareTo(calendarNew) != -1 ? false : true;
    }

    public static String getTodayStart() {
        Calendar today = Calendar.getInstance();
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);
        today.set(Calendar.MILLISECOND, 0);
        return df.format(today.getTime());
    }

    public static String getTodayEnd() {
        Calendar today = Calendar.getInstance();
        today.set(Calendar.HOUR_OF_DAY, 23);
        today.set(Calendar.MINUTE, 59);
        today.set(Calendar.SECOND, 59);
        today.set(Calendar.MILLISECOND, 999);
        return df.format(today.getTime());
    }

    public static String getYesterdayStart() {
        Calendar yesterday = Calendar.getInstance();
        yesterday.add(Calendar.DAY_OF_YEAR, -1);
        yesterday.set(Calendar.HOUR_OF_DAY, 0);
        yesterday.set(Calendar.MINUTE, 0);
        yesterday.set(Calendar.SECOND, 0);
        yesterday.set(Calendar.MILLISECOND, 0);
        return df.format(yesterday.getTime());
    }

    public static String getYesterdayEnd() {
        Calendar yesterday = Calendar.getInstance();
        yesterday.add(Calendar.DAY_OF_YEAR, -1);
        yesterday.set(Calendar.HOUR_OF_DAY, 23);
        yesterday.set(Calendar.MINUTE, 59);
        yesterday.set(Calendar.SECOND, 59);
        yesterday.set(Calendar.MILLISECOND, 999);
        return df.format(yesterday.getTime());
    }

    public static String getYesterday() {
        Calendar yesterday = Calendar.getInstance();
        yesterday.add(Calendar.DAY_OF_YEAR, -1);
        return getYYMMDD(yesterday.getTime());
    }

    /**
     * 今日时间
     */
    public static String getToday() {
        return getYYMMDD(new Date());
    }

    public static String getYYMMDD(Date date) {
        SimpleDateFormat yyMMdd = new SimpleDateFormat("yyyy-MM-dd");
        return yyMMdd.format(date);
    }
    
    public static String getYYMMDDHH(Date date) {
        SimpleDateFormat yyMMdd = new SimpleDateFormat("yyyy-MM-dd-HH");
        return yyMMdd.format(date);
    }
    
    public static String getYYMMDDHHmm(Date date) {
        SimpleDateFormat yyMMdd = new SimpleDateFormat("yyyy-MM-dd-HH:mm");
        return yyMMdd.format(date);
    }
    
    public static String getNowHHmmss() {
        SimpleDateFormat yyMMdd = new SimpleDateFormat("HH:mm:ss");
        return yyMMdd.format(new Date());
    }

    /**
     * 获取前几天或者未来几天的日期
     * @param days
     * @return
     * @throws ParseException
     */
    public static Date getDateByTime(int days) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + days);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
        String result = format.format(today);
        return format.parse(result);
    }

    /**
     * 获取几年前/后的日期
     * @param year
     * @return
     * @throws ParseException
     */
    public static Date getDateByYear(String time , int year) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate parse = LocalDate.parse(time, fmt);
        LocalDate plus= parse.plus(year, ChronoUnit.YEARS);
        return localDate2Date(plus);
    }


    public static Date getDateByMonths(String time , int months) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate parse = LocalDate.parse(time, fmt);
        LocalDate plus= parse.plus(months, ChronoUnit.MONTHS);
        return localDate2Date(plus);
    }

    public static Date localDate2Date(LocalDate localDate) {
        if(null == localDate) {
            return null;
        }
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
    }

    public static String getHH(String time) throws ParseException {
        SimpleDateFormat yyMMdd = new SimpleDateFormat("yyyy-MM-dd-HH");
        Date date = yyMMdd.parse(time);
        SimpleDateFormat hh = new SimpleDateFormat("HH:00");
        return hh.format(date);
    }


    public static Date dateAdd(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, days);
        return calendar.getTime();
    }

    /**
     * parse
     */
    public static Date parse(String time, String pattern) {
        SimpleDateFormat yyMMdd = new SimpleDateFormat(pattern);
        try {
            return yyMMdd.parse(time);
        } catch (ParseException e) {
            log.error("Fail to parse.", e);
            return null;
        }
    }
}
